package com.gitee.wsl.doc.prosemirror.model.content

import com.gitee.wsl.api.IntRangeAble
import com.gitee.wsl.doc.prosemirror.model.node.Node

// Represents a flat range of content, i.e. one that starts and ends in the same node.
data class NodeRange(
    // Construct a node range. `$from` and `$to` should point into the same node until at least the
    // given `depth`, since a node range denotes an adjacent set of nodes in a single parent node.

    // A resolved position along the start of the content. May have a `depth` greater than this
    // object's `depth` property, since these are the positions that were used to compute the range,
    // not re-resolved positions directly at its boundaries.
    val from: ResolvedPos,
    // A position along the end of the content. See caveat for [`$from`](#model.NodeRange.$from).
    val to: ResolvedPos,
    // The depth of the node that this range points into.
    val depth: Int
): IntRangeAble {

    constructor(range: ResolvedPosRange,depth: Int):this(range._from,range._to,depth)

    val resolvedPosRange:ResolvedPosRange get() = from to to

    // The position at the start of the range.
    val start: Int
        get() = this.from.before(this.depth + 1)

    // The position at the end of the range.
    val end: Int
        get() = this.to.after(this.depth + 1)

    override val range: IntRange
        get() = start .. end

    // The parent node that the range points into.
    val parent: Node
        get() = this.from.node(this.depth)

    // The start index of the range in the parent node.
    val startIndex: Int
        get() = this.from.index(this.depth)

    // The end index of the range in the parent node.
    val endIndex: Int
        get() = this.to.indexAfter(this.depth)
}